using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SaiDaoASRClient_Online;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
    public Button startButton;
    private WSClient_Online asrClient;
    private bool isListening = false; // Track whether we are currently listening
    // Start is called before the first frame update
    void StartASRClient()
     {
         Debug.Log("Starting ASR Client...");
         // 调用 FunASR_Main 方法来启动 ASR 功能
         asrClient.FunASR_Main();
         isListening = true; // Update the listening status
         Debug.Log("ASR Client is now active, please speaking");
     }
    
    void ToggleASRListening()
    {
        if (!isListening)
        {
            StartASRClient();
        }
        else
        {
            StopASRClient();
        }
    }
    
    void StopASRClient()
    {
        Debug.Log("Stopping ASR Client but keeping the connection open...");
        // Call method to stop ASR functionality but keep connection
        // asrClient.StopReceiving();  // Assume method to stop receiving but keep connection
        isListening = false; // Update the listening status
        Debug.Log("ASR Client has stopped listening.");
    }

    private void Start()
    {
        // 初始化 ASR 客户端
        asrClient = new WSClient_Online();
        startButton.onClick.AddListener(ToggleASRListening);
    }
    
    private void OnDestroy()
    {
        startButton.onClick.RemoveListener(ToggleASRListening);
        if (asrClient != null)
        {
            asrClient.CloseConnection();
        }
    }
}
